package com.swind.fast.modules.app.railway.project.listener;

import com.swind.fast.common.util.oConvertUtils;

import com.swind.fast.modules.app.railway.project.component.RailwayComponent;
import com.swind.fast.modules.app.railway.task.constant.TaskPointStatusEnum;
import com.swind.fast.modules.app.railway.task.vo.RailwayTaskStatusVo;
import com.swind.fast.modules.app.railway.task.vo.RailwayTestPointStatusVo;
import com.swind.fast.modules.app.railway.task.event.ProjectTaskStatusEvent;
import com.swind.fast.modules.app.railway.task.event.ProjectTaskPointStatusEvent;
import com.swind.fast.modules.app.railway.project.entity.RailwayProjectWorkTask;
import com.swind.fast.modules.app.railway.project.entity.RailwayProject;
import com.swind.fast.modules.app.railway.project.entity.RailwayProjectOperationLog;
import com.swind.fast.modules.app.railway.project.entity.RailwayProjectTaskPoint;

import com.swind.fast.modules.app.railway.project.service.IRailwayProjectService;
import com.swind.fast.modules.app.railway.project.service.IRailwayProjectOperationLogService;
import com.swind.fast.modules.app.railway.project.service.IRailwayProjectWorkTaskService;
import com.swind.fast.modules.app.railway.project.service.IRailwayProjectTaskPointService;
import com.swind.fast.modules.app.railway.task.service.IRailwayTaskPointOperationLogService;
import com.swind.fast.modules.app.railway.task.entity.RailwayTaskPointOperationLog;

import com.swind.fast.modules.app.railway.report.service.IRailwayReportTaskFinishedService;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.scheduling.annotation.Async;
import org.springframework.context.event.EventListener;

import org.springframework.context.ApplicationEvent;

import java.util.List;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class ProjectTaskListener {

    // @Autowired
    // private RailwayComponent railwayComponent;

    @Autowired
    private IRailwayProjectWorkTaskService railwayProjectTaskService;

    @Autowired
    private IRailwayProjectTaskPointService railwayProjectTaskPointService;

    @Autowired
    private IRailwayReportTaskFinishedService railwayReportTaskFinishedService;

    // @Autowired
    // private IRailwayProjectOperationLogService railwayProjectOperationLogService;

    @Autowired
    private IRailwayTaskPointOperationLogService railwayTaskPointOperationLogService;

    @Async
    @EventListener
    public void projectTaskStatusListener(ProjectTaskStatusEvent event) {
        log.info("projectTaskStatusListener");

        RailwayTaskStatusVo taskStatus = (RailwayTaskStatusVo) event.getSource();
        String projectId = taskStatus.getProjectId();
        String holeName = taskStatus.getHoleName();
        List<String> listHoleName = taskStatus.getListHoleName();
        log.info("taskStatus: {}", taskStatus);
        TaskPointStatusEnum status = taskStatus.getTaskPointStatusEnum();

        RailwayProjectWorkTask workTask = railwayProjectTaskService.getWorkTaskByHoleName(projectId, holeName);
        if (oConvertUtils.isEmpty(workTask)) {
            // TODO: 处理任务书逻辑
            return;
        }

        TaskPointStatusEnum ostatus = TaskPointStatusEnum.getByValue(String.valueOf(workTask.getStatus()));
        if (status.equals(TaskPointStatusEnum.UPLOADED)) {
            if (ostatus.equals(TaskPointStatusEnum.TASKED))
                workTask.setStatus(TaskPointStatusEnum.UPLOADED.getValue());
        } else {
            workTask.setStatus(status.getValue());
        }
        railwayProjectTaskService.updateById(workTask);

        // RailwayTaskStatusVo projectStatus = (RailwayTaskStatusVo) event.getSource();
        // String projectId = projectStatus.getProjectId();
        // TaskPointStatusEnum status = projectStatus.getTaskPointStatusEnum();

        // RailwayProject project = railwayProjectService.getById(projectId);
        // Integer fstatus = project.getStatus();
        // Integer pstatus = status.getValue();
        // log.info("project: {}, status: {}", project, pstatus);

        // // UpdateWrapper<RailwayProject> itemWrapper = new UpdateWrapper<RailwayProject>().set("status", pstatus).eq("id", id);
        // // railwayProjectService.update(itemWrapper);

        // project.setStatus(pstatus);
        // railwayProjectService.updateById(project);

        // String memo = projectStatus.getMemo();
        // String createBy = projectStatus.getCreateBy();
        // RailwayProjectOperationLog operationLog = new RailwayProjectOperationLog(projectId, fstatus, pstatus, memo, createBy);
        // railwayProjectOperationLogService.save(operationLog);

        // log.info("username: {}", railwayComponent.getUserRealname());
    }


    @Async
    @EventListener
    public void projectTaskPointStatusListener(ProjectTaskPointStatusEvent event) {
        RailwayTestPointStatusVo taskPointStatus = (RailwayTestPointStatusVo) event.getSource();
        log.info("projectTaskPointPointStatusListener: {}", taskPointStatus);

        String pointId = taskPointStatus.getPointId();
        String createBy = taskPointStatus.getCreateBy();

        RailwayProjectTaskPoint testPoint = railwayProjectTaskPointService.getById(pointId);
        TaskPointStatusEnum ostatus = TaskPointStatusEnum.getByValue(String.valueOf(testPoint.getStatus()));
        TaskPointStatusEnum tstatus = taskPointStatus.getTestPointStatusEnum();
        // testPoint.setStatus(tstatus.getValue());
        // if (ostatus.equals(TaskPointStatusEnum.APPROVING)) {
        //     testPoint.setReviewBy(createBy);
        // }
        // if (tstatus.equals(TaskPointStatusEnum.ANALYZED)) {
        //     testPoint.setCheckBy(createBy);
        // }
        // railwayProjectTaskPointService.updateById(testPoint);

        String memo = taskPointStatus.getMemo();

        RailwayTaskPointOperationLog operationLog = new RailwayTaskPointOperationLog(pointId, ostatus.getValue(), tstatus.getValue(), memo, createBy);
        railwayTaskPointOperationLogService.save(operationLog);

        // 处理竣工报表
        if (tstatus.equals(TaskPointStatusEnum.APPROVED)) {
            String testPointId = taskPointStatus.getPointId();
            railwayReportTaskFinishedService.generateTestFinshedPointReport(testPointId);
        }
    }

}
